#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

char* share_memory = NULL;

int main(){
    int shmid;
    key_t key = ftok("..", 2021);
    if((shmid = shmget(key, 4096, IPC_CREAT | 0666)) == -1){
        perror("shmget");
        exit(1);
    }
    if((share_memory = shmat(shmid, NULL, 0)) == NULL){
        perror("shmat");
        exit(1);
    }
    printf("%p\n", share_memory);
    
    pid_t pid;
    if((pid = fork()) < 0){
        perror("fork");
        exit(1);
    }

    if(pid == 0){
        memset(share_memory, 0, 4096);
        printf("%p\n", share_memory);
        sprintf(share_memory, "hello world\n");
    }
    else{
        wait(NULL);
        printf("%p\n", share_memory);
        printf("%s", share_memory);
        shmdt(share_memory);
    }

    return 0;
}